package com.judysen.jsqlweb.services;

import com.judysen.jsqlweb.aop.JsqlLoggerAnnotation;
import com.judysen.jsqlweb.enums.DataSourceType;
import com.judysen.jsqlweb.exceptions.ServiceException;
import com.judysen.jsqlweb.models.Pager;
import com.judysen.jsqlweb.mybatis.dto.ColumnInfo;
import com.judysen.jsqlweb.mybatis.mapper.BaseMapper;
import com.judysen.jsqlweb.utils.JsqlContext;
import com.judysen.jsqlweb.utils.JsqlDynamicDatasource;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class JsqlManagerService {
    /**
     * 获取注入的具体Mapper
     *
     * @return
     */
    private synchronized BaseMapper getMapper() {
        var config = JsqlDynamicDatasource.getCurrentDb();
        if (config == null) {
            throw new ServiceException(500, "找不到数据库配置信息！");
        }

        int dbType = config.getDataSourceType();
        String beanName = "mysql";
        if (dbType == DataSourceType.Mariadb.getValue()) {
            beanName = "mysql";
        } else if (dbType == DataSourceType.MySql.getValue()) {
            beanName = "mysql";
        } else if (dbType == DataSourceType.MsSql.getValue()) {
            beanName = "mssql";
        } else if (dbType == DataSourceType.Oracle.getValue()) {
            beanName = "oracle";
        } else if (dbType == DataSourceType.PgSql.getValue()) {
            beanName = "pgsql";
        } else {
            beanName = "mysql";
        }
        return JsqlContext.getBean(beanName, BaseMapper.class);
    }

    /**
     * 获取所有的表
     *
     * @return
     */
    @JsqlLoggerAnnotation
    public List<String> getAllTables() {
        var config = JsqlDynamicDatasource.getCurrentDb();
        return getMapper().getTableNames(config.getDatabaseName());
    }

    /**
     * 获取所有的表结构
     *
     * @param table
     * @return
     */
    @JsqlLoggerAnnotation
    public List<ColumnInfo> getColumns(String table) {
        return getMapper().getColumnInfos(table);
    }

    @JsqlLoggerAnnotation
    public int insert(String table, Map<String, Object> columns) {
        var flag = getMapper().save(table, columns);
        return flag;
    }

    /**
     * @param table
     * @param columns
     * @return
     */
    @JsqlLoggerAnnotation
    public int update(String table, Object id, Map<String, Object> columns) {
        return getMapper().update(table, id, columns);
    }

    /**
     * @param filter
     * @return
     */
    @JsqlLoggerAnnotation
    public Pager findByPage(Pager filter) {
        var mapper = getMapper();
        var maps = mapper.selectPage(filter.getTable(), filter.getStartIndex(), filter.getPageSize(), filter.getFilter());
        var totalCount = mapper.selectTotalCount(filter.getTable(), filter.getFilter());

        filter.setRecords(maps);
        filter.setTotalCount(totalCount);

        return filter;
    }

    /**
     * @param table
     * @param id
     * @return
     */
    public Map<String, Object> findOne(String table, Serializable id) {
        return getMapper().selectOne(table, id);
    }

    public int delete(String table, Serializable id) {
        return getMapper().delete(table, id);
    }
}
